﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Auction
{
    public partial class FSearchClient : Form
    {
        private EDM_AuctionContainer context;
        FormBase frmSuper;
        Bidder bidderFound = null;
        public FSearchClient(FormBase frmSuper, EDM_AuctionContainer context)
        {
            InitializeComponent();
            this.frmSuper = frmSuper;
            this.context=context;
        }

        private void rdbSearchByPassport_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchByPassport.Checked)
            {
                grb_Passport.Enabled = true;
                b_SearchClient.Enabled = true;
            }
            else
            {
                grb_Passport.Enabled = false;
            }
        }

        private void rdbSearchByName_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchByName.Checked)
            {
                grb_Name.Enabled = true;
                b_SearchClient.Enabled = true;
            }
            else
            {
                grb_Name.Enabled = false;
            }
        }

        private void rdbSearchByAccount_CheckedChanged(object sender, EventArgs e)
        {
            if (rdbSearchByAccount.Checked)
            {
                grb_Account.Enabled = true;
                b_SearchClient.Enabled = true;
            }
            else
            {
                grb_Account.Enabled = false;
            }
        }

        private void b_SearchClient_Click(object sender, EventArgs e)
        {
            lblAdvNotFound.Visible=false;
            bidderFound = null;
            if (rdbSearchByPassport.Checked)
            {
                try
                {
                    bidderFound = context.BidderSet.FirstOrDefault(b => b.Passport == t_Passport.Text);
                }
                catch (System.Exception)
                {
                    bidderFound = null;
                }
               
            }
            else if(rdbSearchByAccount.Checked)
            {
                try
                {
                    bidderFound = context.BidderSet.FirstOrDefault(b => b.Account == t_Account.Text);
                }
                catch (System.Exception)
                {
                    bidderFound = null;
                }
            }
            else if(rdbSearchByName.Checked)
            {
                try
                {
                    bidderFound = context.BidderSet.FirstOrDefault(b => b.nameBidder == t_Name.Text);
                }
                catch (System.Exception)
                {
                    bidderFound = null;
                }
            }
            if (bidderFound!=null)
            {
                ((FormBase)this.frmSuper).TransferBidderFound(bidderFound);;
            }
            else
            {
                lblAdvNotFound.Visible = true;
            }
        }

    }
}
